package com.hzhk.happyrent.ui.activity.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.product.OrderListModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.DataNullAdapter;
import com.hzhk.happyrent.ui.adapter.MoreServiceAdapter;
import com.hzhk.happyrent.ui.adapter.ProductOrderAdapter;
import com.hzhk.happyrent.ui.view.listview.MaiTianListview;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderListActivity extends AppCompatActivity implements MaiTianListview.OnMaiTianRefreshListener {

    @BindView(R.id.lv_order_list)
    MaiTianListview lvOrderList;
    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.tab_select1)
    TextView tabSelect1;
    @BindView(R.id.tab_select2)
    TextView tabSelect2;
    @BindView(R.id.tab_select3)
    TextView tabSelect3;

    private String orderState;
    private ProductOrderAdapter productOrderAdapter;
    //    private String[] strings = new String[]{"全部", "待付款", "待发货", "待收货", "已完成"};
    private String[] strings = new String[]{"进行中", "已完成", "已取消"};
    private String[] orderStatus = new String[]{"process", "finish", "cancel"};
    private int page = 1;
    private List<OrderListModel.DataBean.OrdersBean> ordersBeans = new ArrayList<>();
    private DataNullAdapter dataNullAdapter = new DataNullAdapter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        activityTitlebarLeft.setImageResource(R.drawable.back);
        activityTitlebarCenter.setText(R.string.orders_all);
//        activityTitlebarRight.setVisibility(View.VISIBLE);
        activityTitlebarRight.setImageDrawable(getResources().getDrawable(R.drawable.service));
        tabSelect1.setSelected(true);
        lvOrderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(!ListUtils.isEmpty(ordersBeans)&&position>0&&position-1<ordersBeans.size()){
                    Intent intent = new Intent(OrderListActivity.this, OrderDetailActivity.class);
                    String order_state = (ordersBeans.get(position-1).getOrder_state_current());
                    intent.putExtra("order_id", ordersBeans.get(position - 1).getId());
                    intent.putExtra("order_state", order_state);
                    startActivity(intent);
                }
            }
        });
        lvOrderList.setOnMaiTianRefreshListener(this);
        productOrderAdapter = new ProductOrderAdapter(this);
        lvOrderList.setAdapter(productOrderAdapter);
    }

    private void initData() {
        Map<String, Object> params = new HashMap<>();
        params.put(Constants.USER_ID, MyPerference.getInstance().getUserId());
        params.put(Constants.PAGE, page);
        if (!TextUtils.isEmpty(orderState)) {
            params.put(Constants.ORDER_TYPE, orderState);
        }
        params.put(Constants.LIMIT, Constants.PAGE_SIZE);
        HttpManager.getInstance().get(API.GET_MY_ORDERS, params, new MyStringResponse(this) {
            @Override
            public void onCompleted(Object tag) {
                super.onCompleted(tag);
                lvOrderList.stop();
            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                OrderListModel orderListModel = new Gson().fromJson(response, OrderListModel.class);
                if (page == 1) {
                    ordersBeans.clear();
                    if (ListUtils.isEmpty(orderListModel.getData().getOrders())) {
                        lvOrderList.setFooterVisible(false);
                        lvOrderList.setAdapter(dataNullAdapter);
                    } else {
                        lvOrderList.setFooterVisible(true);
                        lvOrderList.setAdapter(productOrderAdapter);
                    }
                }
                if (!ListUtils.isEmpty(orderListModel.getData().getOrders())) {
                    ordersBeans.addAll(orderListModel.getData().getOrders());
                    productOrderAdapter.setList(ordersBeans);
                }
                if (orderListModel.getData().getOrders().size() < Constants.PAGE_SIZE) {
                    lvOrderList.allLoadFinish();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 1;
        initData();
    }

    @Override
    public void onLoadMore() {
        page++;
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        onRefresh();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK&&productOrderAdapter != null && productOrderAdapter.getOrderCancelDialog() != null && productOrderAdapter.getOrderCancelDialog().isShowing()) {
            productOrderAdapter.getOrderCancelDialog().closeCancel();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.activity_titlebar_left,R.id.tab_select1, R.id.tab_select2, R.id.tab_select3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.tab_select1:
                if(!tabSelect1.isSelected()){
                    tabSelect1.setSelected(true);
                    tabSelect2.setSelected(false);
                    tabSelect3.setSelected(false);
                    tabSelect1.setTextColor(getResources().getColor(R.color.tab_red_80));
                    tabSelect2.setTextColor(getResources().getColor(R.color.black_40));
                    tabSelect3.setTextColor(getResources().getColor(R.color.black_40));
                    orderState = orderStatus[0];
                    page = 1;
                    initData();
                }
                break;
            case R.id.tab_select2:
                if(!tabSelect2.isSelected()){
                    tabSelect1.setSelected(false);
                    tabSelect2.setSelected(true);
                    tabSelect3.setSelected(false);
                    tabSelect1.setTextColor(getResources().getColor(R.color.black_40));
                    tabSelect2.setTextColor(getResources().getColor(R.color.tab_red_80));
                    tabSelect3.setTextColor(getResources().getColor(R.color.black_40));
                    orderState = orderStatus[1];
                    page = 1;
                    initData();
                }
                break;
            case R.id.tab_select3:
                if(!tabSelect3.isSelected()){
                    tabSelect1.setSelected(false);
                    tabSelect2.setSelected(false);
                    tabSelect3.setSelected(true);
                    tabSelect1.setTextColor(getResources().getColor(R.color.black_40));
                    tabSelect2.setTextColor(getResources().getColor(R.color.black_40));
                    tabSelect3.setTextColor(getResources().getColor(R.color.tab_red_80));
                    orderState = orderStatus[2];
                    page = 1;
                    initData();
                }
                break;
        }
    }
}
